﻿using Dapper;
using OMG.Fill.Vegetable.Model.Ent;
using ProjectShare;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
namespace OMG.Fill.Vegetable.Service
{
    /// <summary>
    /// 表5蔬菜生产经营实体及相关情况表T_VegProsecution通用操作类
    /// </summary>
    public class VegProsecutions
    {
        /// <summary>
        /// 添加
        /// </summary>
        public virtual ResResult<int> AddInfo(VegProsecutionEntity model)
        {
            ResResult<int> obj = new ResResult<int>();
            string strSql = @"INSERT INTO dbo.T_VegProsecution";
            strSql += "(";
            strSql += "Id,";
            strSql += "ReportId,";
            strSql += "PFSC_Num,";
            strSql += "PFSC_Cd,";
            strSql += "PFSC_Zy,";
            strSql += "PFSC_Qy,";
            strSql += "VegCompanyNum,";
            strSql += "ZzNum_Country,";
            strSql += "ZzNum_Pro,";
            strSql += "ZzNum_City,";
            strSql += "ZzNum_Area,";
            strSql += "ZzNum_Other,";
            strSql += "ZzNum_yye1,";
            strSql += "ZzNum_yye2,";
            strSql += "ZzNum_yye3,";
            strSql += "ZzNum_yye4,";
            strSql += "ZzNum_yye5,";
            strSql += "ZzNum_yye6,";
            strSql += "YmqyNum,";
            strSql += "Ymqy1,";
            strSql += "Ymqy2,";
            strSql += "Ymqy3,";
            strSql += "VegHzNum,";
            strSql += "VegNhNum,";
            strSql += "VegMjNum,"; 
             strSql += "VegHzSale,";
             strSql += "JTNC_Num,";
            strSql += "JTNC_NMNum,";
            strSql += "JTNC_Area,";
            strSql += "JTNC_Sales,";
            strSql += "ZlrzNum_Sfx,";
            strSql += "ZlrzNum_Sfq,";
            strSql += "ZlrzNum_Cmsbs,";
            strSql += "ZlrzNum_Mpcp,";
            strSql += "ZlrzNum_YnsZmsb,";
            strSql += "ZlrzNum_WghNum,";
            strSql += "ZlrzNum_Wghmj,";
            strSql += "ZlrzNum_Greencp,";
            strSql += "ZlrzNum_Greencpmj,";
            strSql += "ZlrzNum_Dlsb,";
            strSql += "ZlrzNum_Dlbh,";
            strSql += "Remark,";
            strSql += "AddUser,";
            strSql += "AddTime,";
            strSql += "UpdateUser,";
            strSql += "UpdateTime)";
            strSql += " values(";
            strSql += "@Id,";
            strSql += "@ReportId,";
            strSql += "@PFSC_Num,";
            strSql += "@PFSC_Cd,";
            strSql += "@PFSC_Zy,";
            strSql += "@PFSC_Qy,";
            strSql += "@VegCompanyNum,";
            strSql += "@ZzNum_Country,";
            strSql += "@ZzNum_Pro,";
            strSql += "@ZzNum_City,";
            strSql += "@ZzNum_Area,";
            strSql += "@ZzNum_Other,";
            strSql += "@ZzNum_yye1,";
            strSql += "@ZzNum_yye2,";
            strSql += "@ZzNum_yye3,";
            strSql += "@ZzNum_yye4,";
            strSql += "@ZzNum_yye5,";
            strSql += "@ZzNum_yye6,";
            strSql += "@YmqyNum,";
            strSql += "@Ymqy1,";
            strSql += "@Ymqy2,";
            strSql += "@Ymqy3,";
            strSql += "@VegHzNum,";
            strSql += "@VegNhNum,";
            strSql += "@VegMjNum,"; 
             strSql += "@VegHzSale,";
             strSql += "@JTNC_Num,";
            strSql += "@JTNC_NMNum,";
            strSql += "@JTNC_Area,";
            strSql += "@JTNC_Sales,";
            strSql += "@ZlrzNum_Sfx,";
            strSql += "@ZlrzNum_Sfq,";
            strSql += "@ZlrzNum_Cmsbs,";
            strSql += "@ZlrzNum_Mpcp,";
            strSql += "@ZlrzNum_YnsZmsb,";
            strSql += "@ZlrzNum_WghNum,";
            strSql += "@ZlrzNum_Wghmj,";
            strSql += "@ZlrzNum_Greencp,";
            strSql += "@ZlrzNum_Greencpmj,";
            strSql += "@ZlrzNum_Dlsb,";
            strSql += "@ZlrzNum_Dlbh,";
            strSql += "@Remark,";
            strSql += "@AddUser,";
            strSql += "@AddTime,";
            strSql += "@UpdateUser,";
            strSql += "@UpdateTime)";
            try
            {
                using (var dbCon = ConnectFactory.GetOpenConnection())
                {
                    model.Id = CustomFun.GetGuid();
                    int result = dbCon.Execute(strSql, model);
                    if (result > 0)
                    {
                        obj.model = result;
                        obj.status = DefaultKey.SuccessResult;
                        obj.message = "添加成功！";
                    }
                    else
                    {
                        obj.model = result;
                        obj.status = DefaultKey.InfoResult;
                        obj.message = "添加失败！数量为:" + result;
                    }
                    dbCon.Dispose();//数据库连接释放
                    return obj;
                }
            }
            catch (Exception ex)
            {
                obj.status = DefaultKey.ErrorResult;
                obj.message = ex.ToString().Trim();
                LogHelper.ErrorLog("新增表5蔬菜生产经营实体及相关情况表T_VegProsecution对象信息失败", ex);
                return obj;
            }
        }
        /// <summary>
        /// 修改
        /// </summary>
        public virtual ResResult<int> UpdateInfo(VegProsecutionEntity model)
        {
            ResResult<int> obj = new ResResult<int>();
            ReportServices reportServices = new ReportServices();
            string strSql = @"UPDATE dbo.T_VegProsecution set ";
            strSql += "ReportId = @ReportId,";
            strSql += "PFSC_Num = @PFSC_Num,";
            strSql += "PFSC_Cd = @PFSC_Cd,";
            strSql += "PFSC_Zy = @PFSC_Zy,";
            strSql += "PFSC_Qy = @PFSC_Qy,";
            strSql += "VegCompanyNum = @VegCompanyNum,";
            strSql += "ZzNum_Country = @ZzNum_Country,";
            strSql += "ZzNum_Pro = @ZzNum_Pro,";
            strSql += "ZzNum_City = @ZzNum_City,";
            strSql += "ZzNum_Area = @ZzNum_Area,";
            strSql += "ZzNum_Other = @ZzNum_Other,";
            strSql += "ZzNum_yye1 = @ZzNum_yye1,";
            strSql += "ZzNum_yye2 = @ZzNum_yye2,";
            strSql += "ZzNum_yye3 = @ZzNum_yye3,";
            strSql += "ZzNum_yye4 = @ZzNum_yye4,";
            strSql += "ZzNum_yye5 = @ZzNum_yye5,";
            strSql += "ZzNum_yye6 = @ZzNum_yye6,";
            strSql += "YmqyNum = @YmqyNum,";
            strSql += "Ymqy1 = @Ymqy1,";
            strSql += "Ymqy2 = @Ymqy2,";
            strSql += "Ymqy3 = @Ymqy3,";
            strSql += "VegHzNum = @VegHzNum,";
            strSql += "VegNhNum = @VegNhNum,";
            strSql += "VegMjNum = @VegMjNum,"; 
             strSql += "VegHzSale = @VegHzSale,";
             strSql += "JTNC_Num = @JTNC_Num,";
            strSql += "JTNC_NMNum = @JTNC_NMNum,";
            strSql += "JTNC_Area = @JTNC_Area,";
            strSql += "JTNC_Sales = @JTNC_Sales,";
            strSql += "ZlrzNum_Sfx = @ZlrzNum_Sfx,";
            strSql += "ZlrzNum_Sfq = @ZlrzNum_Sfq,";
            strSql += "ZlrzNum_Cmsbs = @ZlrzNum_Cmsbs,";
            strSql += "ZlrzNum_Mpcp = @ZlrzNum_Mpcp,";
            strSql += "ZlrzNum_YnsZmsb = @ZlrzNum_YnsZmsb,";
            strSql += "ZlrzNum_WghNum = @ZlrzNum_WghNum,";
            strSql += "ZlrzNum_Wghmj = @ZlrzNum_Wghmj,";
            strSql += "ZlrzNum_Greencp = @ZlrzNum_Greencp,";
            strSql += "ZlrzNum_Greencpmj = @ZlrzNum_Greencpmj,";
            strSql += "ZlrzNum_Dlsb = @ZlrzNum_Dlsb,";
            strSql += "ZlrzNum_Dlbh = @ZlrzNum_Dlbh,";
            strSql += "Remark = @Remark,";
            strSql += "AddUser = @AddUser,";
            strSql += "AddTime = @AddTime,";
            strSql += "UpdateUser = @UpdateUser,";
            strSql += "UpdateTime = @UpdateTime";
            strSql += " where Id = @Id";
            try
            {
                using (var dbCon = ConnectFactory.GetOpenConnection())
                {
                    int result = dbCon.Execute(strSql, model);
                    int resultReport = reportServices.UpStatus(model.ReportId, "2", "").status; //更新当前报表状态为已填报
                    if (result > 0 && resultReport == 1)
                    {
                        obj.model = result;
                        obj.status = DefaultKey.SuccessResult;
                        obj.message = "数据更新成功！";
                    }
                    else
                    {
                        obj.model = result;
                        obj.status = DefaultKey.InfoResult;
                        obj.message = "更新失败！数量为:" + result;
                    }
                    dbCon.Dispose();//数据库连接释放
                }
            }
            catch (Exception ex)
            {
                obj.status = DefaultKey.ErrorResult;
                obj.message = ex.ToString().Trim();
                LogHelper.ErrorLog("更新表5蔬菜生产经营实体及相关情况表T_VegProsecution对象信息失败", ex);
            }
            return obj;
        }
        /// <summary>
        /// 删除
        /// </summary>
        public virtual ResResult<int> DeleteInfo(string id)
        {
            ResResult<int> obj = new ResResult<int>();
            string strSql = @"delete from dbo.T_VegProsecution where Id = @Id";
            int result;
            try
            {
                using (var dbCon = ConnectFactory.GetOpenConnection())
                {
                    result = dbCon.Execute(strSql, new { Id = id });
                    if (result > 0)
                    {
                        obj.model = result;
                        obj.status = DefaultKey.SuccessResult;
                        obj.message = "删除成功！";
                    }
                    else
                    {
                        obj.model = result;
                        obj.status = DefaultKey.InfoResult;
                        obj.message = "删除失败！数量为:" + result;
                    }
                    dbCon.Dispose();//数据库连接释放
                    return obj;
                }
            }
            catch (Exception ex)
            {
                obj.status = DefaultKey.ErrorResult;
                obj.message = ex.ToString().Trim();
                LogHelper.ErrorLog("删除表5蔬菜生产经营实体及相关情况表T_VegProsecution对象信息失败", ex);
                return obj;
            }
        }
        /// <summary>
        /// 批量删除
        /// </summary>
        public virtual ResResult<int> DeleteBatch(string[] ids)
        {
            ResResult<int> obj = new ResResult<int>();
            string strIds = string.Join(",", ids.Select(x => x.ToString()).ToArray());
            string strSql = @"delete from dbo.T_VegProsecution where Id in @ids";
            var dynamicSqlParam = new DynamicParameters();
            dynamicSqlParam.Add("ids", ids.ToArray(), null, null, null);
            int result;
            try
            {
                using (var dbCon = ConnectFactory.GetOpenConnection())
                {
                    result = dbCon.Execute(strSql, dynamicSqlParam);
                    if (result > 0)
                    {
                        obj.model = result;
                        obj.status = DefaultKey.SuccessResult;
                        obj.message = "删除成功！";
                    }
                    else
                    {
                        obj.status = DefaultKey.InfoResult;
                        obj.message = "删除失败！数量为:" + result;
                    }
                    dbCon.Dispose();//数据库连接释放
                    return obj;
                }
            }
            catch (Exception ex)
            {
                LogHelper.ErrorLog("获取数据字典表批量删除表5蔬菜生产经营实体及相关情况表T_VegProsecution表失败", ex);
                obj.message = ex.ToString().Trim();
                obj.status = DefaultKey.ErrorResult;
                return obj;
            }
        }
        /// <summary>
        /// 获取指定对象
        /// </summary>
        public virtual ResResult<VegProsecutionEntity> GetModel(string id)
        {
            ResResult<VegProsecutionEntity> obj = new ResResult<VegProsecutionEntity>();
            string strSql = @"select * from dbo.T_VegProsecution where Id = @Id";
            try
            {
                using (var dbCon = ConnectFactory.GetOpenConnection())
                {
                    obj.model = dbCon.Query<VegProsecutionEntity>(strSql, new { Id = id }).FirstOrDefault();
                    obj.status = DefaultKey.SuccessResult;
                    obj.message = "获取成功！";
                    dbCon.Dispose();//数据库连接释放
                    return obj;
                }
            }
            catch (Exception ex)
            {
                obj.status = DefaultKey.ErrorResult;
                obj.message = ex.ToString().Trim();
                LogHelper.ErrorLog("获取表5蔬菜生产经营实体及相关情况表T_VegProsecution对象信息失败", ex);
                return obj;
            }
        }
        /// <summary>
        /// 获取全部列表
        /// </summary>
        /// <returns></returns>
        public virtual ResResult<List<VegProsecutionEntity>> GetList()
        {
            ResResult<List<VegProsecutionEntity>> obj = new ResResult<List<VegProsecutionEntity>>();
            string strSql = @"select * from dbo.T_VegProsecution";
            try
            {
                using (var dbCon = ConnectFactory.GetOpenConnection())
                {
                    obj.model = dbCon.Query<VegProsecutionEntity>(strSql).ToList();
                    obj.status = DefaultKey.SuccessResult;
                    obj.message = "获取成功！";
                    dbCon.Dispose();//数据库连接释放
                    return obj;
                }
            }
            catch (Exception ex)
            {
                obj.status = DefaultKey.ErrorResult;
                obj.message = ex.ToString().Trim();
                LogHelper.ErrorLog("更新表5蔬菜生产经营实体及相关情况表T_VegProsecution对象信息失败", ex);
                return obj;
            }
        }
    }
}